package webservices;

import helpers.KontrolerPristupa;

import java.util.List;

import dao.DAOFactory;
import dao.KorisnikDAO;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; 
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import sun.security.krb5.Credentials;
import classes.Korisnik;

public class KorisnikResource {

@Path("/korisnici")
public class KorisniciResurs {
	  @Context
	  UriInfo uriInfo;
	  @Context
	  Request request;
 
	 
	  @GET
	  @Path("login")
	  @Produces(MediaType.APPLICATION_XML)
	  public Korisnik prijaviNaSistem(@HeaderParam("username")String username, @HeaderParam("password")String password) throws Exception{
		  try{
			  DAOFactory.connect();
			  Korisnik k = KontrolerPristupa.getKorisnik(username, password);
			  DAOFactory.disconnect();
			  return k;
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return null;
		  }
	  }
	  
	  
	  @GET
	  @Path("logout")
	  @Produces(MediaType.APPLICATION_XML)
	  public String OdjaviSaSistema(@HeaderParam("username")String username, @HeaderParam("password")String password){
		  try{
			  DAOFactory.connect();
			  int i = KontrolerPristupa.getID(username, password);
			  if (i!=-1){
				 
				  DAOFactory.disconnect();
				  return Boolean.toString(true);
			  }
			  return Boolean.toString(false);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 	  
	  
	  //getAllKorisnici
	  @GET
	  @Path("svi")
	  @Produces(MediaType.APPLICATION_XML)
	  public GenericEntity<List<Korisnik>> getAllKorisnici() throws Exception {
		  try{
			  DAOFactory.connect();
			  List<Korisnik> korisnici = DAOFactory.getKorisnikDAO().selectSviKorisnici();
			  DAOFactory.disconnect();
			  return new GenericEntity<List<Korisnik>>(korisnici){}; 
		  }
		  catch(Exception e)
		  { 
			  e.printStackTrace();
			  return null;
		  }		  
	  }
	  /*
	  @GET
	  @Path("korisnik/credentials/userpass")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.TEXT_XML)
	  public CredentialsReply generirajPodatkeZaKorisnika(@HeaderParam("username")String username, @HeaderParam("password")String password, @HeaderParam("ime")String ime, @HeaderParam("prezime") String prezime) throws Exception
	  {
		  try{
			  DAOFactory.connect();
			  int idKorisnikaAdmina = KontrolerPristupa.getID(username, password, Korisnik.Privilegije.Administrator);
			  if (idKorisnikaAdmina!=-1){
				  String uname = Credentials.generirajUsername(ime,prezime);
				  String pw = Credentials.generirajPassword();
				  DAOFactory.disconnect();
				  return new CredentialsReply(uname,pw);
				  
			  }
			  return null;
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return null;
		  }
	  }*/
	  /*
	  @GET
	  @Path("korisnik/credentials/pass")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String getPassword(@HeaderParam("username")String username, @HeaderParam("password")String password){
		  try{
			  DAOFactory.connect();
			  int idKorisnikaAdmina = KontrolerPristupa.getID(username, password, Korisnik.Privilegije.Administrator);
			  DAOFactory.disconnect();
			  if (idKorisnikaAdmina!=-1){
				  return Credentials.generirajPassword(); 
			  }
			  return null;
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return null;
		  }
	  }*/
	  
	  //novi korisnik
	  @PUT
	  @Path("korisnik/novi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putKorisnik(Korisnik k) throws Exception {
		  try{
			  DAOFactory.connect();
				  DAOFactory.getKorisnikDAO().insertKorisnik(k);
				  DAOFactory.disconnect();
				  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	  /*
	  @GET
	  @Path("sausernameom")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public GenericEntity<List<Korisnik>> getKorisnikeDjelomicniUsername(@HeaderParam("username")String username, @HeaderParam("password")String password, String partialUsername){
		  try{
			  DAOFactory.connect();
			  int idKorisnikaAdmina = KontrolerPristupa.getID(username, password, Korisnik.Privilegije.Administrator);
			  if (idKorisnikaAdmina!=-1){
				  List<Korisnik> pronadjeniKorisnici = DAOFactory.getKorisnikDAO().selectKorisnici(partialUsername);
				  DAOFactory.disconnect();
				  return new GenericEntity<List<Korisnik>>(pronadjeniKorisnici){};
			  }
			  return null;
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return null;
		  }
	  }*/
	  
	  //update
	  @POST
	  @Path("korisnik/promijeni")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String postKorisnik(@HeaderParam("username")String username, @HeaderParam("password")String password, Korisnik k){
		  try{
			  DAOFactory.connect();
				  int i = DAOFactory.getKorisnikDAO().updateKorisnik(k);
				  if (i!=1){
					  DAOFactory.disconnect();
					  return Boolean.toString(false);
				  }
				  DAOFactory.disconnect();
				  return Boolean.toString(true);
		  }
		  catch (Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	  
	  //deleteKorisnik
	  @DELETE
	  @Path("korisnik/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deleteKorisnik(String sIDKorisnika){
		  try{
			  int idKorisnika = Integer.parseInt(sIDKorisnika);
			  DAOFactory.connect();
				  int i = DAOFactory.getKorisnikDAO().deleteKorisnik(idKorisnika);
				  if (i!=1){
					  DAOFactory.disconnect();
					  return Boolean.toString(false);
				  }
				  DAOFactory.disconnect();
				  return Boolean.toString(true);		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	}
}

